package com.ojoin.trade.supplyloan_core.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.controller.BaseController;
import com.ojoin.trade.common.web.controller.BaseControllerImpl;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
import com.ojoin.trade.supplyloan_core.web.service.TbCatchdataStrategyService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyPaymentService;

@Controller
@RequestMapping("/operate/tbslsupplypayment")
public class TbSlSupplyPaymentController extends
		BaseControllerImpl<TbSlSupplyPayment> implements
		BaseController<TbSlSupplyPayment> {

	private Logger log = LoggerFactory
			.getLogger(TbSlSupplyPaymentController.class);

	@Autowired
	private TbSlSupplyPaymentService service;

	@Autowired
	private TbSlPlatformUserService tbSlPlatformUserService;

	@Autowired
	private TbCatchdataStrategyService catchdataStrategyService;
	
	@Override
	protected BaseService<TbSlSupplyPayment> getBaseService() {

		return service;
	}

	@ResponseBody
	@RequestMapping(value = "/querybytbslsupplypayment", method = RequestMethod.GET)
	// type 区别是取全量值还是差值（all,change）
	public Result querybytbslsupplypayment(
			int platformAccountId,String startDate,String endDate,String type, String settlementState,Pageable page) {
		try {
			log.info(platformAccountId+"----"+"statDate"+startDate+"------endDate"+endDate+"-----settlementState"+settlementState);
            
			TbCatchdataStrategy  ca = catchdataStrategyService.queryByPlatformUserId(platformAccountId);
			List<TbSlSupplyPayment> li = null;
			if("all".equals(type)){
				if(ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)){
					// 全量查询初始化开始时间 
					startDate = endDate;
					li = service.queryByUidAndRowCreate(platformAccountId, startDate, endDate,settlementState,page);
				}else {
					// 增量查询初始化开始时间
					startDate = null;
					li = service.queryByUidAndCreateDate(platformAccountId, startDate, endDate,settlementState,page);
				}
				
				return new Result(Result.Status.OK, li);
				
			}else if("change".equals(type)){
				if(ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)){
					li = service.queryByUidAndCreateDateAndRowCreate(platformAccountId, startDate, endDate, endDate, endDate, settlementState, page);
				}else {
					li = service.queryByUidAndCreateDate(platformAccountId, startDate, endDate,settlementState,page);
				}
				
				return new Result(Result.Status.OK, li);
			} if("newpay".equals(type)){
				if(ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)){
					li = service.queryByUidAndCreateDateAndRowCreate(platformAccountId, startDate, endDate, endDate, endDate, settlementState, page);
				}else {
					li = service.queryByUidAndRowUpdate(platformAccountId, startDate, endDate, settlementState, page);
				}
				
				return new Result(Result.Status.OK, li);
			}else {
				return new Result(Result.Status.ERROR, "type类型出错");
			}
			

			
		} catch (Exception e) {
			log.error("", e);
			return new Result(Result.Status.ERROR, "查询电商业务数据统计失败");
		}

	}

	/*
	 * 电商业务数据统计
	 */
	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping(value = "/querybytbslsupplypayment", method =
	 * RequestMethod.GET) public ModelAndView
	 * querySupplyChainPlatformUserCanNotLogin( int platformAccountId) {
	 * ModelAndView mv = new ModelAndView();
	 * log.info("platformAccountId:"+platformAccountId);
	 * mv.addObject("platformAccountId",platformAccountId);
	 * mv.setViewName("operate/tbslsupplypaymentlist.jsp"); try {
	 * List<TbSlSupplyPayment> li =
	 * service.queryByPlatformAccountId(platformAccountId);
	 * mv.addObject("li",li); TbSlPlatformUser platuser =
	 * tbSlPlatformUserService.queryById(platformAccountId);
	 * mv.addObject("li",li); mv.addObject("platuser",platuser ); Map<String,
	 * String> map =IPlatformUrlDef.platformname_NameMap; mv.addObject("map",map
	 * );
	 * 
	 * //return new Result(Result.Status.OK, li); } catch (Exception e) {
	 * log.error("", e); //return new Result(Result.Status.ERROR,
	 * "查询电商业务数据统计失败"); }
	 * 
	 * return mv ; }
	 */

}
